package com.redstar.HappyRefresh.manage;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.redstar.HappyRefresh.authority.Authority;
import com.redstar.HappyRefresh.constant.AuditStatus;
import com.redstar.HappyRefresh.pojo.*;
import com.redstar.HappyRefresh.service.TypeService;
import com.redstar.HappyRefresh.service.UserService;
import com.redstar.HappyRefresh.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/admin/video")
public class VideoManage { // 后台视频管理

    @Autowired
    private VideoService videoService;
    @Autowired
    private UserService userService;
    @Autowired
    private TypeService typeService;

    @GetMapping("/{id}")
    @Authority("admin:video:get")
    public Result get(@PathVariable Long id){ // 通过视频id获取指定视频信息
        return Result.succeed().data(videoService.getVideoById(id, null)); // 后台管理端不用传递用户id
    }

    @GetMapping("/page")
    @Authority("admin:video:page")
    public Result list(BasePage basePage, @RequestParam(required = false) String YV, @RequestParam(required = false) String title){ // 分页模糊查询，用于搜索
        IPage<Video> page = videoService.page(basePage.page(), new LambdaQueryWrapper<Video>().like(!ObjectUtils.isEmpty(YV),Video::getYv,YV).like(!ObjectUtils.isEmpty(title),Video::getTitle,title));
        List<Video> videoList = page.getRecords();
        // 没有视频信息
        if (ObjectUtils.isEmpty(videoList)) {
            return Result.succeed();
        }
        ArrayList<Long> userIds = new ArrayList<>(); // 视频对应的用户id
        ArrayList<Long> typeIds = new ArrayList<>(); // 视频对应的分类id
        for (Video video : videoList) {
            userIds.add(video.getUserId());
            typeIds.add(video.getTypeId());
        }
        // 根据用户id查询用户昵称，封装为map
        Map<Long, String> userMap = userService.list(new LambdaQueryWrapper<User>().select(User::getId, User::getNickName).in(User::getId,userIds))
                .stream().collect(Collectors.toMap(User::getId, User::getNickName));
        // 根据分类id查询分类名称，封装为map
        Map<Long, String> typeMap = typeService.list(new LambdaQueryWrapper<Type>().select(Type::getId, Type::getName).in(Type::getId,typeIds))
                .stream().collect(Collectors.toMap(Type::getId, Type::getName));
        for (Video video : videoList) {
            video.setAuditStateName(AuditStatus.getName(video.getAuditStatus())); // 填充视频当前的审核状态
            video.setUserName(userMap.get(video.getUserId()));
            video.setOpenName(video.getOpen() ? "私密" : "公开"); // 填充视频当前的公开状态
            video.setTypeName(typeMap.get(video.getTypeId()));
        }
        return Result.succeed().data(videoList).count(page.getTotal());
    }

    @DeleteMapping("/{id}")
    @Authority("admin:video:delete")
    public Result delete(@PathVariable Long id){ // 删除视频
        videoService.deleteVideo(id);
        return Result.succeed().message("删除成功");
    }

    @PostMapping("/audit")
    @Authority("admin:video:audit")
    public Result audit(@RequestBody Video video){ // 放行视频
        videoService.auditProcess(video);
        return Result.succeed().message("审核放行");
    }

    @PostMapping("/violations/{id}")
    @Authority("admin:video:violations")
    public Result Violations(@PathVariable Long id){ // 下架视频
        videoService.violations(id);
        return Result.succeed().message("下架成功");
    }

    @GetMapping("/statistics")
    @Authority("admin:video:statistics")
    public Result show(){ // 查询各种状态的视频数量
        int allCount = videoService.count(new LambdaQueryWrapper<>()); // 视频总数
        int processCount = videoService.count(new LambdaQueryWrapper<Video>().eq(Video::getAuditStatus, AuditStatus.PROCESS)); // 审核中
        int successCount = videoService.count(new LambdaQueryWrapper<Video>().eq(Video::getAuditStatus, AuditStatus.SUCCESS)); // 通过
        int passCount = videoService.count(new LambdaQueryWrapper<Video>().eq(Video::getAuditStatus, AuditStatus.PASS)); // 失败
        VideoStatistics videoStatistics = new VideoStatistics();
        videoStatistics.setAllCount(allCount);
        videoStatistics.setPassCount(passCount);
        videoStatistics.setSuccessCount(successCount);
        videoStatistics.setProcessCount(processCount);
        return Result.succeed().data(videoStatistics);
    }
}
